<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\admin\model\Group;
use think\Request;

class Base
{
    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /**
     * 以json格式发送接口返回信息
     * @param string $data
     * @param string $msg
     * @param string $code
     * @return false|string
     */
    protected function sendJson($data = '',$msg = 'success',$code = '000000'){
        return json_encode([
           'data' => $data,
           'msg' => $msg,
           'code' => $code
        ]);
    }



    //拼团流程
    public function group()
    {
        $id = input('id');

        $user_id = input('user_id');

        $groupData = Group::where('id',$id)->find();

        \think\facade\Db::startTrans();

        try {

            \app\admin\model\Order::create([
                'order_no'=>rand(100000000,9999999999),
                'user_id'=>$user_id,
                'goods_id'=>$groupData['goods_id']
                //...
            ]);

            Group::where('id',$id)->update([
                'quantity'=>$groupData['quantity']+1
            ]);

            \think\facade\Db::commit();
            return json_encode(['code'=>200,'data'=>'','msg'=>'拼团成功']);
        }catch (\Exception $exception){
            \think\facade\Db::rollback();
            return json_encode(['code'=>400,'data'=>'','msg'=>'拼团失败']);
        }
    }
}
